/**
 * (./) udp.pde - how to use UDP library as unicast connection
 * (cc) 2006, Cousot stephane for The Atelier Hypermedia
 * (->) http://hypermedia.loeil.org/processing/
 *
 * Create a communication between Processing<->Pure Data @ http://puredata.info/
 * This program also requires to run a small program on Pd to exchange data  
 * (hum!!! for a complete experimentation), you can find the related Pd patch
 * at http://hypermedia.loeil.org/processing/udp.pd
 * 
 * -- note that all Pd input/output messages are completed with the characters 
 * ";\n". Don't refer to this notation for a normal use. --
 */

// import UDP library
import hypermedia.net.*;

float [] px = new float[4];
float [] py = new float[4];
float [] opx = new float[4];
float [] opy = new float[4];

String message  = str( key );	// the message to send
String ip       = "localhost";	// the remote IP address
int port        = 10003;	// the destination port
  
UDP udp;  // define the UDP object


void setup() {
  
  size(600,400);
  frameRate(30);
  
  for (int i=0;i<4;i++)
  {
      px[i] = 100 + (i&1)*300;
      py[i] = 100 + int(i/2)*200;
      opx[i] = px[i];
      opy[i] = py[i];
  }
  
  // create a new datagram connection on port 6000
  // and wait for incomming message
  
  udp = new UDP( this, 6000 );
  
  //udp.log( true ); 		// <-- printout the connection activity
  udp.listen( true );
  
  
  //publish initial values  
    for (int i=0;i<4;i++)
    {
      message = "Publish,";
        message += "var"+nf(i,0,0)+"x,"+nf(px[i]*0.01,0,3);
        // send the message
        udp.send( message, ip, port );
      message = "Publish,";  
        message += "var"+nf(i,0,0)+"y,"+nf((400-py[i])*0.01,0,3);
        // send the message
        udp.send( message, ip, port );
    }
      
}

//process events
void draw() 
{
  background(100);
  
 
  fill(255);
  for (int i=0;i<4;i++)
  {
      rect(px[i],py[i],20,20);
  }
  
  if ((frameCount&15)==0)
  {
    
    for (int i=0;i<4;i++)
    {
      
      message = "Publish,";
      //boolean change = false;
      
      if (!(px[i]==opx[i])) 
      {
        message += "var"+nf(i,0,0)+"x,"+nf(px[i]*0.01,0,3);
        //change = true;
        // send the message
        udp.send( message, ip, port );
      }
      
      message = "Publish,";
      if (!(py[i]==opy[i])) 
      {
        message += "var"+nf(i,0,0)+"y,"+nf((400-py[i])*0.01,0,3);
//        change = true;
        // send the message
        udp.send( message, ip, port );
      }
     
      //remember old values
      opx[i] = px[i];
      opy[i] = py[i];
      
      //if (change) udp.send( message, ip, port );
      
    }
    
    
    
    
  }
}


void mouseDragged()
{

  for (int i=0;i<4;i++)
  {
      if ((mouseX>=px[i])&&(mouseX<=px[i]+20)&&(mouseY>=py[i])&&(mouseY<=py[i]+20))
      {
        px[i] += mouseX-pmouseX;
        py[i] += mouseY-pmouseY;
      }
  }
  
}



/**
 * To perform any action on datagram reception, you need to implement this 
 * handler in your code. This method will be automatically called by the UDP 
 * object each time he receive a nonnull message.
 * By default, this method have just one argument (the received message as 
 * byte[] array), but in addition, two arguments (representing in order the 
 * sender IP address and his port) can be set like below.
 */
// void receive( byte[] data ) { 			// <-- default handler
void receive( byte[] data, String ip, int port ) {	// <-- extended handler
  
  
  // get the "real" message =
  // forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
  data = subset(data, 0, data.length-2);
  String message = new String( data );
  
  // print the result
  println( "receive: \""+message+"\" from "+ip+" on port "+port );
}

